package com.shir.travels.ui.yulun;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.shir.travels.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PinglunRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int VIEW_TYPE = -1;
    public static final int VIEW_LEFT = 0;
    public static final int VIEW_RIGHT = 1;

    public static final int ITEM_IMG = 3;
    public static final int ITEM_MSG = 4;

    private List<Map<Integer, Object>> listItemRes;

    public PinglunRecyclerAdapter() {
        listItemRes = new ArrayList<>();
    }

    public void clear(){
        listItemRes.clear();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == VIEW_LEFT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_item_msg_left, parent, false);
            return new PingLunLeftViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_item_msg_right, parent, false);
            return new PingLunRightHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemType = (Integer) listItemRes.get(position).get(VIEW_TYPE);
        if (itemType == VIEW_LEFT) {
            PingLunLeftViewHolder leftViewHolder = (PingLunLeftViewHolder) holder;
            leftViewHolder.imageView.setImageResource((Integer) listItemRes.get(position).get(ITEM_IMG));
            leftViewHolder.textView.setText(listItemRes.get(position).get(ITEM_MSG) + "");
        } else {
            PingLunRightHolder rightHolder = (PingLunRightHolder) holder;
            rightHolder.imageView.setImageResource((Integer) listItemRes.get(position).get(ITEM_IMG));
            rightHolder.textView.setText(listItemRes.get(position).get(ITEM_MSG) + "");
        }
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return listItemRes.size();
    }

    public List<Map<Integer, Object>> getListItemRes() {
        return listItemRes;
    }

    @Override
    public int getItemViewType(int position) {
        return (Integer) listItemRes.get(position).get(VIEW_TYPE);
    }

    class PingLunLeftViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;

        public PingLunLeftViewHolder(View viewItem) {
            super(viewItem);
            imageView = (ImageView) viewItem.findViewById(R.id.imageview_frag_msg_left);
            textView = (TextView) viewItem.findViewById(R.id.textview_frag_text_left);
        }
    }

    class PingLunRightHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView textView;

        public PingLunRightHolder(View viewItem) {
            super(viewItem);
            imageView = (ImageView) viewItem.findViewById(R.id.imageview_frag_msg_right);
            textView = (TextView) viewItem.findViewById(R.id.textview_frag_text_right);
        }
    }
}
